package com.zh.sharding.sale.repository;

import com.zh.sharding.sale.entity.Product;
import com.zh.sharding.sale.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author ZH
 * @date 09:24 2023/8/4
 */
@Mapper
public interface ProductDao {
    String TABLE_NAME = "t_product";

    @Insert("insert into t_product(id,name,store,price,create_time) " +
            " values(#{id},#{name},#{store},#{price},#{createTime})")
    int insert(Product product);

    @Select("SELECT * FROM t_product WHERE id=#{id}")
    Product selectById(@Param("id") Long id);

    @Select("SELECT * FROM t_product WHERE id=#{id} FOR UPDATE")
    Product selectByIdLock(@Param("id") Long id);


    @Select("SELECT * FROM t_product")
    List<Product> selectList();

    /**
     * 更新库存
     * @param id
     * @param store
     * @return
     */
    @Update("UPDATE t_product SET store=#{store} WHERE id=#{id}")
    int updateStoreById(@Param("id")Long id, @Param("store")int store);

    /**
     * 扣减库存
     * @param id
     * @param num
     * @return
     */
    @Update("UPDATE t_product SET store=store-#{num} WHERE id=#{id} AND store>=#{num}")
    int deduceStoreById(@Param("id") Long id, @Param("num") int num);

}
